home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / alpha_blending / antialias.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.2 KB  |  366 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* antialias.c - shows how to enable antialiasing for points and 
  19.  *        lines
  20.  *
  21.  *  Escape key              - exit the program 
  22.  *  Left Mouse Button        - change incidence and azimuth angles
  23.  *  Middle Mousebutton        - change the twist angle based on
  24.  *                  horizontal mouse movement
  25.  *  Right Mousebutton        - zoom in and out based on vertical
  26.  *                  mouse movement
  27.  *  <a> Key            - toggle antialiasing on / off
  28.  *  <b> key            - toggle blend function
  29.  *  <o> Key            - change the drawing order
  30.  *  <R> key            - reset view
  31.  */
  32. #include <GL/gl.h>
  33. #include <GL/glu.h>
  34. #include <GL/glut.h>
  35. #include <math.h>
  36. #include <stdio.h>    /* for printf */
  37.  
  38. /*  Function Prototypes  */
  39.  
  40. GLvoid  initgfx( GLvoid );
  41. GLvoid  drawScene( GLvoid );
  42. GLvoid  reshape( GLsizei, GLsizei );
  43. GLvoid  keyboard( GLubyte, GLint, GLint );
  44. GLvoid  mouse( GLint, GLint, GLint, GLint );
  45. GLvoid  motion( GLint, GLint );
  46. GLvoid  initialize( char * );
  47. GLvoid  blendFuncCycle( GLvoid );
  48. GLvoid  antialiasing( GLvoid );
  49.  
  50. void resetView( GLvoid );
  51. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  52. void printHelp( char * );
  53.  
  54. /* Global Definitions */
  55.  
  56. #define KEY_ESC    27    /* ascii value for the escape key */
  57.  
  58. /* Global Variables */
  59.  
  60. static GLboolean    antialiasFlag = GL_FALSE;
  61. static GLboolean    drawBackToFront = GL_TRUE;
  62.  
  63. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  64. static GLint        action;
  65.  
  66. static GLint        xStart = 0, yStart = 0;
  67.  
  68. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  69.  
  70. GLvoid
  71. main( int argc, char *argv[] )
  72. {
  73.     GLsizei width, height;
  74.  
  75.     glutInit( &argc, argv );
  76.  
  77.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  78.     height = glutGet( GLUT_SCREEN_HEIGHT );
  79.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  80.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  81.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
  82.     glutCreateWindow( argv[0] );
  83.  
  84.     initgfx();
  85.  
  86.     glutMouseFunc( mouse );
  87.     glutMotionFunc( motion );
  88.     glutKeyboardFunc( keyboard );
  89.     glutReshapeFunc( reshape );
  90.     glutDisplayFunc( drawScene ); 
  91.  
  92.     printHelp( argv[0] );
  93.  
  94.     glutMainLoop();
  95. }
  96.  
  97. void
  98. printHelp( char *progname )
  99. {
  100.     fprintf(stdout, "\n%s - demonstrates point and line antialiasing\n\n"
  101.         "Axes: X - red, Y - green, Z - blue\n\n"
  102.         "Left Mousebutton    - move eye position\n"
  103.         "Middle Mousebutton    - change twist angle\n"
  104.         "Right Mousebutton    - move up / down to zoom in / out\n"
  105.         "<a> Key        - toggle antialiasing on / off\n"
  106.         "<b> key        - toggle blend function\n"
  107.         "<o> Key        - change the drawing order\n"
  108.         "<R> Key        - reset view\n"
  109.         "Escape Key        - exit the program\n\n",
  110.         progname);
  111. }
  112.  
  113. GLvoid
  114. initgfx( GLvoid )
  115. {
  116.     GLfloat maxObjectSize = 3.0;
  117.  
  118.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  119.     glShadeModel( GL_FLAT );
  120.  
  121.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  122.     /* and determine the viewing distance (adjust for zooming) */
  123.     near = 1.0;
  124.     far = near + 8*maxObjectSize; 
  125.  
  126.     resetView();
  127.  
  128.     /* set the initial blend function */
  129.     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  130.     fprintf(stdout, 
  131.         "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
  132.  
  133.     /* make the point size large so that the points 
  134.      * don't disappear when antialiasing is enabled */
  135.     glPointSize( 5.5 );
  136.     glLineWidth( 4.5 );
  137.  
  138.     glEnable( GL_DEPTH_TEST );
  139.     printf("depth buffer is enabled\n");
  140. }
  141.  
  142. GLvoid
  143. reshape( GLsizei width, GLsizei height )
  144. {
  145.     GLdouble            aspect;
  146.  
  147.     glViewport( 0, 0, width, height );
  148.  
  149.     aspect = (GLdouble) width / (GLdouble) height;
  150.  
  151.     glMatrixMode( GL_PROJECTION );
  152.     glLoadIdentity();
  153.     gluPerspective( 45.0, aspect, near, far );
  154.     glMatrixMode( GL_MODELVIEW );
  155. }
  156.  
  157. GLvoid  
  158. antialiasing( GLvoid )
  159. {
  160.     antialiasFlag = !antialiasFlag;
  161.  
  162.     /* Toggle anti-aliasing for each type of primitive */
  163.     if ( antialiasFlag == GL_TRUE ) {
  164.         glEnable( GL_POINT_SMOOTH );
  165.         glEnable( GL_LINE_SMOOTH );
  166.     } else {
  167.         glDisable( GL_POINT_SMOOTH );
  168.         glDisable( GL_LINE_SMOOTH );
  169.     }
  170.     printf("antialiasing %s\n", (antialiasFlag? "ON" : "OFF")); 
  171. }
  172.  
  173. GLvoid  
  174. blendFuncCycle( GLvoid )
  175. {
  176.     static int whichBlendFunc = 0;
  177.     
  178.     whichBlendFunc = (whichBlendFunc + 1) % 2;
  179.     
  180.     switch (whichBlendFunc)
  181.     {
  182.     case 0:
  183.         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  184.         fprintf(stdout, 
  185.             "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
  186.         break;
  187.     case 1:
  188.         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
  189.         fprintf(stdout, 
  190.             "glBlendFunc( GL_SRC_ALPHA, GL_ONE )\n" );
  191.         break;
  192.     default:
  193.         break;
  194.     }
  195. }
  196.  
  197. GLvoid 
  198. keyboard( GLubyte key, GLint x, GLint y )
  199. {
  200.     switch (key) {
  201.     case 'a':    /* toggle antialiasing */
  202.         antialiasing();
  203.         glutPostRedisplay();
  204.         break;
  205.     case 'o':    /* change drawing order */
  206.         drawBackToFront = !drawBackToFront;    
  207.         printf("drawing order: %s\n", 
  208.             (drawBackToFront? "Back to Front" : "Front to Back")); 
  209.         glutPostRedisplay();
  210.         break;
  211.     case 'b':    /* cycle through several blend funcs */
  212.         blendFuncCycle();
  213.         glutPostRedisplay();
  214.         break;
  215.     case 'R':
  216.         resetView();
  217.         glutPostRedisplay();
  218.         break;
  219.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  220.         exit(0);
  221.     }
  222. }
  223.  
  224. GLvoid 
  225. mouse( GLint button, GLint state, GLint x, GLint y )
  226. {
  227.     static GLint buttons_down = 0;
  228.  
  229.     if (state == GLUT_DOWN) {
  230.         switch (button) {
  231.         case GLUT_LEFT_BUTTON:
  232.             action = MOVE_EYE;
  233.             break;
  234.         case GLUT_MIDDLE_BUTTON:
  235.             action = TWIST_EYE;
  236.             break;
  237.         case GLUT_RIGHT_BUTTON:
  238.             action = ZOOM;
  239.             break;
  240.         }
  241.  
  242.         /* Update the saved mouse position */
  243.         xStart = x;
  244.         yStart = y;
  245.     } else {
  246.         if (--buttons_down == 0) 
  247.             action = MOVE_NONE;
  248.     }
  249.  
  250. }
  251.  
  252. GLvoid
  253. motion( GLint x, GLint y )
  254. {
  255.     switch (action) {
  256.     case MOVE_EYE:
  257.         /* Adjust the eye position based on the mouse position */
  258.         azimAngle += (GLdouble) (x - xStart);
  259.         incAngle -= (GLdouble) (y - yStart);
  260.         break;
  261.     case TWIST_EYE:
  262.         /* Adjust the eye twist based on the mouse position */
  263.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  264.         break;
  265.     case ZOOM:
  266.         /* Adjust the eye distance based on the mouse position */
  267.         distance -= (GLdouble) (y - yStart)/10.0;
  268.         break;
  269.     default:
  270.         printf("unknown action %d\n", action);
  271.     }
  272.     
  273.     /* Update the stored mouse position for later use */
  274.     xStart = x;
  275.     yStart = y;
  276.  
  277.     glutPostRedisplay();
  278. }
  279.  
  280. void
  281. resetView( GLvoid )
  282. {
  283.     distance = near + (far - near) / 2.0;
  284.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  285.     incAngle = 0.0;
  286.     azimAngle = 0.0;
  287. }
  288.  
  289. void
  290. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  291.             GLfloat twist)
  292. {
  293.     glTranslatef( 0.0, 0.0, -distance);
  294.     glRotatef( -twist, 0.0, 0.0, 1.0);
  295.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  296.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  297. }
  298.  
  299. void
  300. drawBackRectangle( GLvoid )
  301. {
  302.     static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  303.  
  304.     glColor4fv( magenta );
  305.     glutSolidCube( 2.0 );
  306. }
  307.  
  308.  
  309. void
  310. drawAntialiasedObjects()
  311. {
  312.     static GLfloat    yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  313.  
  314.     if ( antialiasFlag == GL_TRUE ) {
  315.         /* enable blending */
  316.         glEnable( GL_BLEND );
  317.     }
  318.  
  319.     /* draw a point in front of the cube */
  320.     glPushMatrix();
  321.         glTranslatef( 0.0, 0.0, 1.1 );
  322.  
  323.         /* Draw a point */
  324.         glColor4fv( yellow );
  325.         glBegin( GL_POINTS );
  326.             glVertex2f( 0.0, 0.5 );
  327.         glEnd();
  328.     glPopMatrix();
  329.  
  330.     /* Draw a wireframe icosahedron surrounding the cube */
  331.     glPushMatrix();
  332.         glColor4fv( yellow );
  333.         glScalef( 2.0, 2.0, 2.0 );
  334.         glutWireIcosahedron();
  335.     glPopMatrix();
  336.  
  337.     if ( antialiasFlag == GL_TRUE ) {
  338.         /* disable blending */
  339.         glDisable( GL_BLEND );
  340.     }
  341. }
  342.  
  343. GLvoid
  344. drawScene( GLvoid )
  345. {
  346.     
  347.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  348.  
  349.     glPushMatrix();
  350.         polarView( distance, azimAngle, incAngle, twistAngle );
  351.  
  352.         glRotatef( 15.0, 1.0, 1.0, 0.0 );
  353.  
  354.         if ( drawBackToFront ) {
  355.             drawBackRectangle();
  356.             drawAntialiasedObjects();
  357.         } else {
  358.             drawAntialiasedObjects();
  359.             drawBackRectangle();
  360.         }
  361.  
  362.     glPopMatrix();
  363.  
  364.     glutSwapBuffers();
  365. }
  366.